import store from '@/store'
import { App } from 'vue'
// 根据权限判断是否显示  v-has

const install = (app: App) => {
  app.directive('has', {
    mounted(el, binding) {
      const { value } = binding
      const userInfo: string[] = (store.getters as any)['user/roles']
      if (value && value instanceof Array && value.length > 0) {
        if (!(userInfo.indexOf('admin') >= 0)) {
          const permissionRoles = value

          const hasPermission = userInfo.some(role => {
            return permissionRoles.includes(role)
          })

          if (!hasPermission) {
            el.parentNode && el.parentNode.removeChild(el)
          }
        }
      } else {
        throw new Error('使用方式： v-has="[\'admin\',\'editor\']"')
      }
    }
  })
}

export default install
